package com.yanqu.road.dao.impl.channelcenter.washe;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.washe.data.WasheLastResultData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class WasheLastResultDataDaoImpl extends TempDataObjectDao<WasheLastResultData> {

    public static String getWasheLastResultDataSql = "select * from `t_u_washe_last_result` where `userId`=?";

    public WasheLastResultData getWasheLastResultData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getWasheLastResultDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateWasheLastResultDataSql = "update `t_u_washe_last_result` set `season`=?,`msgData`=?,`handle`=?,`read`=?,`time`=? where `userId`=?";

    @Override
    protected boolean updateData(WasheLastResultData washeLastResultData) {
        washeLastResultData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, washeLastResultData.getSeason()));
        para.put(i++, new DbParameter(Types.VARCHAR, washeLastResultData.getMsgData()));
        para.put(i++, new DbParameter(Types.BOOLEAN, washeLastResultData.isHandle()));
        para.put(i++, new DbParameter(Types.BOOLEAN, washeLastResultData.isRead()));
        para.put(i++, new DbParameter(Types.BIGINT, washeLastResultData.getTime()));
        para.put(i++, new DbParameter(Types.BIGINT, washeLastResultData.getUserId()));
        if (getDb().executeNoneQuery(updateWasheLastResultDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertWasheLastResultDataSql = "insert into `t_u_washe_last_result`(`userId`,`season`,`msgData`,`handle`,`read`,`time`) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(WasheLastResultData washeLastResultData) {
        washeLastResultData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, washeLastResultData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, washeLastResultData.getSeason()));
        para.put(i++, new DbParameter(Types.VARCHAR, washeLastResultData.getMsgData()));
        para.put(i++, new DbParameter(Types.BOOLEAN, washeLastResultData.isHandle()));
        para.put(i++, new DbParameter(Types.BOOLEAN, washeLastResultData.isRead()));
        para.put(i++, new DbParameter(Types.BIGINT, washeLastResultData.getTime()));
        if (getDb().executeNoneQuery(insertWasheLastResultDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected WasheLastResultData parseData(ResultSet rs) throws SQLException {
        WasheLastResultData washeLastResultData = new WasheLastResultData();
        washeLastResultData.setUserId(rs.getLong("userId"));
        washeLastResultData.setSeason(rs.getInt("season"));
        washeLastResultData.setMsgData(rs.getString("msgData"));
        washeLastResultData.setHandle(rs.getBoolean("handle"));
        washeLastResultData.setRead(rs.getBoolean("read"));
        washeLastResultData.setTime(rs.getLong("time"));
        return washeLastResultData;
    }

}
